package com.wtp.wallpaper.rabbitmq.consumer;

import com.wtp.wallpaper.common.constant.RabbitmqConstant;
import com.wtp.wallpaper.es.document.PictureDocument;
import com.wtp.wallpaper.es.service.PictureDocumentService;
import com.wtp.wallpaper.pojo.entity.Picture;
import com.wtp.wallpaper.rabbitmq.data.DataChangeEvent;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Slf4j
@Component
public class PictureMessageConsumer {

    @Autowired
    private PictureDocumentService pictureDocumentService;
    @RabbitListener(queues = "es.sync.queue")
    public void handleEvent(DataChangeEvent event) {
        log.info("消费消息:{}",event);
        switch (event.getOperationType()) {
            case RabbitmqConstant.BATCH_CREATE:
                pictureDocumentService.saveAll(event.getDataList(PictureDocument.class));
                break;
            case RabbitmqConstant.BATCH_DELETE:
                pictureDocumentService.deleteByIds((List<Long>)event.getData());
                break;
            case RabbitmqConstant.UPDATE:
                pictureDocumentService.update((PictureDocument) event.getData(PictureDocument.class));
                break;
        }
    }
}
